//
//  GGTWeiBoDetailTableViewCell.m
//  GGT
//
//  Created by Vision on 14-11-9.
//  Copyright (c) 2014年 GGT. All rights reserved.
//

#import "UIImageView+WebCache.h"
#import "GGTWeiBoDetailTableViewCell.h"

@implementation GGTWeiBoDetailTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.profile_image_urlView = [[UIImageView alloc]init];
        [self.contentView addSubview:self.profile_image_urlView];
        self.nameLabel = [[UILabel alloc]init];
        self.nameLabel.font = [UIFont fontWithName:@".HelveticaNeueInterface-Regular" size:19];
        [self.contentView addSubview:self.nameLabel];
        self.created_atLabel = [[UILabel alloc]init];
        self.created_atLabel.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:self.created_atLabel];
        self.sourceLabel  = [[UILabel alloc]init];
        self.sourceLabel.textColor = [UIColor orangeColor];
        [self.contentView addSubview:self.sourceLabel];
        self.weiboTextLabel  = [[UILabel alloc]init];
        //self.weiboTextLabel.textColor = [UIColor orangeColor];
        [self.contentView addSubview:self.weiboTextLabel];
        
        self.retweetedView = [[UIView alloc]init];
        
        [self.contentView addSubview:self.retweetedView];
        
        self.retweeted_text = [[UILabel alloc]init];
        self.retweeted_text.textColor = [UIColor darkGrayColor];
        [self.retweetedView addSubview:self.retweeted_text];
        
////引用微博view
//@property (nonatomic, retain) UIView *retweetedView;
////引用微博
//@property (nonatomic, retain) UILabel *retweeted_text;
////引用图片
//@property (nonatomic, retain) UIImageView *retweeted_picView;
////引用图片数组
//@property (nonatomic, retain) NSArray *retweeted_picArr;
//@property (nonatomic, retain) UIView *retweeted_pic_View;
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    //清除上次的图片view
    [self.pic_view removeFromSuperview];
    [self.retweeted_pic_View removeFromSuperview];
    
    //创建微博正文图片容器
    self.pic_view = [[UIView alloc]init];
    [self.contentView addSubview:self.pic_view];
    //创建微博引用图片容器
    self.retweeted_pic_View = [[UIView alloc]init];
    [self.retweetedView addSubview:self.retweeted_pic_View];
    //创建头像位置
    self.profile_image_urlView.frame = CGRectMake(4, 4, 50, 50);
    //名字位置
    self.nameLabel.frame = CGRectMake(60, 4, 200, 25);
    //微博发表时间位置
    self.created_atLabel.frame = CGRectMake(60, 34, 100, 20);
    //微博来源位置
    self.sourceLabel.frame = CGRectMake(170, 34, 150, 20);
    //微博正文位置
    self.weiboTextLabel.frame = CGRectMake(4, 60, self.frame.size.width - 4, self.frame.size.height);
    self.weiboTextLabel.numberOfLines = 0;
    [self.weiboTextLabel sizeToFit];
    //微博正文图片容器位置
    if (self.picArr.count == 1) {
        self.pic_view.frame = CGRectMake(0, self.weiboTextLabel.frame.size.height + 60, self.contentView.frame.size.width, 100);
    } else if (self.picArr.count > 1 && self.picArr.count <= 3){
        self.pic_view.frame = CGRectMake(0, self.weiboTextLabel.frame.size.height + 60, self.contentView.frame.size.width, 70);
    } else if (self.picArr.count > 3 && self.picArr.count <= 6){
        self.pic_view.frame = CGRectMake(0, self.weiboTextLabel.frame.size.height + 60, self.contentView.frame.size.width, 74 + 70);
    } else {
        self.pic_view.frame = CGRectMake(0, self.weiboTextLabel.frame.size.height + 60, self.contentView.frame.size.width, 74 * 2 + 70);
    }
    //创建微博正文图片 及其位置
    [self creatImageView:self.picArr view:self.pic_view imageView:self.pic_urlsView];
   
    //引用文字
    self.retweeted_text.frame = CGRectMake(4, 0, 320, 40);
    self.retweeted_text.numberOfLines = 0;
    [self.retweeted_text sizeToFit];
    
    //引用图片容器的位置
    if (self.retweeted_picArr.count == 1) {
        
        self.retweeted_pic_View.frame = CGRectMake(0, self.retweeted_text.frame.size.height, 320, 100);
    } else if (self.retweeted_picArr.count > 1 && self.retweeted_picArr.count <= 3) {
        self.retweeted_pic_View.frame = CGRectMake(0, self.retweeted_text.frame.size.height, 320,  70);
        
    }else if (self.retweeted_picArr.count > 3 && self.retweeted_picArr.count <= 6) {
        self.retweeted_pic_View.frame = CGRectMake(0, self.retweeted_text.frame.size.height, 320, 70 + 74);
        
    }else {
        self.retweeted_pic_View.frame = CGRectMake(0, self.retweeted_text.frame.size.height, 320, 70 + 74 * 2);
    }
    
    //创建微博引用图片 及其位置
    [self creatImageView:self.retweeted_picArr view:self.retweeted_pic_View imageView:self.retweeted_picView];
    
    //引用view
    self.retweetedView.frame = CGRectMake(0, self.pic_view.frame.origin.y , 320, self.retweeted_text.frame.size.height );
//    self.retweetedView.backgroundColor = [UIColor lightGrayColor];
//    self.retweeted_pic_View.backgroundColor = [UIColor lightGrayColor];

   
}
- (void)creatImageView:(NSArray *)arr view:(UIView *)v imageView:(UIImageView *)imageV;
{
    //图片
    int i = 0;
    if (arr.count == 0) {
        
    } else if (arr.count == 1){
        for (NSDictionary * temp in arr) {
            imageV = [[UIImageView alloc]init];
            imageV.frame = CGRectMake(4 + i * 74, 0 , 100, 100);
            [imageV setImageWithURL:[NSURL URLWithString:[temp objectForKey:@"thumbnail_pic"]]placeholderImage:[UIImage imageNamed:@"hold.png"]];
            [v addSubview: imageV];
            i++;
        }
    }
    else if (arr.count > 1 && arr.count <=3) {
        for (NSDictionary * temp in arr) {
            imageV = [[UIImageView alloc]init];
            imageV.frame = CGRectMake(4+ i * 74, 0 , 70, 70);
            [imageV setImageWithURL:[NSURL URLWithString:[temp objectForKey:@"thumbnail_pic"]]placeholderImage:[UIImage imageNamed:@"hold.png"]];
            [v addSubview: imageV];
            i++;
        }
        
    } else if (arr.count > 3 && arr.count <= 6) {
        for (int j = 0; j < 3; j++) {
            imageV = [[UIImageView alloc]init];
            imageV.frame = CGRectMake(4+ i * 74 , 0, 70, 70);
            [imageV setImageWithURL:[NSURL URLWithString:[arr[j] objectForKey:@"thumbnail_pic"]]placeholderImage:[UIImage imageNamed:@"hold.png"]];
            [v addSubview: imageV];
            i++;
        }
        i = 0;
        for (int j = 3; j < arr.count; j++) {
            imageV = [[UIImageView alloc]init];
            imageV.frame = CGRectMake(4 + i * 74, 74, 70, 70);
            [imageV setImageWithURL:[NSURL URLWithString:[arr[j] objectForKey:@"thumbnail_pic"]]placeholderImage:[UIImage imageNamed:@"hold.png"]];
            [v addSubview: imageV];
            i++;
        }
    } else {
        for (int j = 0; j < 3; j++) {
            imageV = [[UIImageView alloc]init];
            imageV.frame = CGRectMake(4 + i * 74, 0, 70, 70);
            [imageV setImageWithURL:[NSURL URLWithString:[arr[j] objectForKey:@"thumbnail_pic"]]placeholderImage:[UIImage imageNamed:@"hold.png"]];
            [v addSubview: imageV];
            i++;
        }
         i = 0;
        for (int j = 3; j < 6; j++) {
            imageV = [[UIImageView alloc]init];
            imageV.frame = CGRectMake(4 + i * 74, 74, 70, 70);
            [imageV setImageWithURL:[NSURL URLWithString:[arr[j] objectForKey:@"thumbnail_pic"]]placeholderImage:[UIImage imageNamed:@"hold.png"]];
            [v addSubview: imageV];
            i++;
        }
         i = 0;
        for (int j = 6; j < arr.count; j++) {
            imageV = [[UIImageView alloc]init];
            imageV.frame = CGRectMake(4+ i * 74 , 2 * 74, 70, 70);
            [imageV setImageWithURL:[NSURL URLWithString:[arr[j] objectForKey:@"thumbnail_pic"]]placeholderImage:[UIImage imageNamed:@"hold.png"]];
            [v addSubview: imageV];
            i++;
        }
    }

}
- (void)dealloc
{
    [_profile_image_urlView release];
    [_nameLabel release];
    [_created_atLabel release];
    [_sourceLabel release];
    [_weiboTextLabel release];
    [_pic_urlsView release];
    [_picArr release];
    [_retweeted_text release];
    [_retweeted_picView release];
    [_retweeted_picArr release];
    [super dealloc];
}
- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
